<?php
/**
 * @filesource modules/comment/controllers/IndexController.php
 * @author Dao Quang Duc
 * @abstract : List of actions in order
 *   - new
 *   - view
 *   - edit
 *   - delete
 *   - search
 */ 
class Comment_MasterController extends Clu_Controller_Action_Master
{
	public $m, $mPlace, $mArticle;
	
	public function init()
	{
		//require_once (APP_PATH.'/modules/place/Model/Place.php');
		//require_once (APP_PATH.'/modules/event/Model/Event.php');
		//require_once (APP_PATH.'/modules/article/Model/Article.php');
		$this->mPlace = new place_Model_Place();
		$this->mArticle = new article_Model_Service();
		$this->mEvent = new event_Model_Service();
		$this->m = new comment_Model_Service();
		
		parent::init();
	}
	
	public function editAction() 
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
    	//check user rights
//       	if (strpos ($sess->userPermissions, 'update comment') === false)
//	    {
//	    	$this->viewParams['errorMessage'] = t(68);
//    	 	$this->renderError(null,t(504));
//            return;
//	    }
	    
		$commentId = $this->_request->getParam("id");
		if (!$c = $this->m->findByPrimary($commentId))
        {
            $this->viewParams['errorMessage'] = __(603);
            $this->renderError(null,t(504));
            return;
        }	
        
        //everything's fine. Continue editing
        $this->viewParams['form'] = new comment_Form_Comment();
//        $this->viewParams['form']->cluBuild(array('action' => 'update','control' => 'index', 'view' => $this->view));
        
        $actionUrl = $this->view->hvnUrl(array('module' => 'comment', 'action'=>'edit'), 'nocontroller');
        $this->viewParams['form']->cluBuild(array('action' => 'update', 'action_url' => $actionUrl, 
        												  'control' => 'index',
                                                          'userPermissions' => $sess->userPermissions, 
                                                          'language' => $sess->viewLang ));
        
        //populate the data with 
		if (!$this->_request->isPost())
            $this->viewParams['form']->populate($c->toArray());
        else
        { 
            if ($this->viewParams['form']->isValid($_POST))
            {	
            	
                if ($this->m->saveRaw($c, $this->_request->getParams(), $commentId, false))
                {
                    $this->viewParams['operation'] = __("update comment");
                    //$this->renderToHolder('common/success.phtml');
                    return;
                }
                else 
                    $this->viewParams['errorMessage'] = __(607);
            }
            else 
            	$this->viewParams['errorMessage'] = __(71);
        }
        
        $this->viewParams['id'] = $commentId;
        //$this->renderToHolder('index/edit.phtml');    
	}
	
	public function viewAction() 
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		$data = $this->_request->getParams();
//		die_a($data);
		//Search comment form
		$this->viewParams['formSearch'] = new comment_comment_Form_CommentSearch();
//        $this->viewParams['formSearch']->cluBuild(array('control' => 'service'));
        $actionUrl = "/" . $sess->viewLang . "/comment/ajax/search-by-module";
        $this->viewParams['formSearch']->cluBuild(array('action' => 'view' ,'action_url' => $actionUrl, 
        												  'control' => 'service',
                                                           ));
        
		//new Comment Box
		$this->viewParams['form2'] = new comment_Form_Comment();
//		$this->viewParams['form2']->cluBuild(array('action' => 'view','control' => 'ajax'));
		
		$actionUrl = "/" . $sess->viewLang . "/comment/ajax/new-comment";
        $this->viewParams['form2']->cluBuild(array('action' => 'view' , 'action_url' => $actionUrl, 
        												  'control' => 'ajax',
                                                           ));
		
		//comment list + pagination
       	$select = $this->m->search($data);
		$paginator = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($select));
		$paginator->setItemCountPerPage($data['comments_per_page']);
		$paginator->setCurrentPageNumber($data['page_number']);
		Zend_Paginator::setDefaultScrollingStyle('Jumping');
		$this->viewParams['paginator'] = $paginator;
		$this->viewParams['cList'] = $paginator->getCurrentItems();
		//TODO: Set page number to HTML
		//TODO: just a hack to render the content
		//TODO: rewrite comment module and also rewrite widget calling widget for viewParams
		//$this->viewParams['commentList'] = $this->view->render('en/index/commentList.phtml');
		$this->viewParams['commentList'] = 
				$this->view->render('/modules/comment/views/scripts/commonTheme/commonLang/common/commentList.phtml');
				
		//$script = '/modules/' . $this->_request->getModuleName() . '/views/scripts/' . $masterWidgetInfo['script_path'] . ".phtml";
		
		//additional info: parent type, parent id
		$this->viewParams['content_type'] = $data['content_type'];
		$this->viewParams['content_id'] = $data['content_id'];
		//Everything put together
       	//$this->renderToHolder('index/view.phtml');
	}
	
	public function deleteAction() 
	{
		//check user rights
       	if (strpos ($sess->userPermissions, 'administer user') === false)
	    {
	    	$this->viewParams['errorMessage'] = t(68);
    	 	$this->renderError(null,t(504));
            return;
	    }
	    
		if (!$id = $this->_request->getParam('id'))
    	{
    		$this->viewParams['errorMessage'] = __(608);
    		$this->renderError(null,t(504));
            return;
        }
        if ($this->m->deleteByPrimary($id))
        {
            $this->viewParams['operation'] = __(604);
            //$this->renderToHolder( 'common/success.phtml');
        }
		else
		{   
			$this->viewParams['errorMessage'] = __(605);
            $this->renderError(null,t(504));
            return;
		} 
	}
	
	public function searchAction()
    {
    	$data = $this->_request->getParams();
      	$this->viewParams['form'] = new comment_comment_Form_CommentSearch();
    //  	$this->viewParams['form']->cluBuild(array('control' => 'index'));
      	
      	$actionUrl = $this->view->hvnUrl(array('module' => 'comment', 'action'=>'search'), 'nocontroller');
        $this->viewParams['form']->cluBuild(array('action' => 'view' , 'action_url' => $actionUrl, 
        												  'control' => 'index',
                                                           ));
         
		if($this->_request->isPost() && $this->viewParams['form']->isValid($_POST))
		{
	    	if($paginator = $this->m->search($data))
    		{
    			$paginator = $this->generatePaginator($paginator, 1, $data['comments_per_page'], 3, 'Jumping');
                $this->viewParams['form']->populate($data);  
    			
    			//cache in $_SESSION
               	$sess = new Zend_Session_Namespace("comment_paginator");
                $sess->comment_admin = array("search_params" => $data); 
        	}
		}
        else if($data['page'] != null){
			$sess = new Zend_Session_Namespace("comment_paginator");
        	$data = $sess->comment_admin;
           	if(isset($data))
           	{
               $select = $this->m->search($data['search_params']);
               $paginator = $this->generatePaginator($select, $this->_getParam('page'), $data['search_params']['comments_per_page'], 3, 'Jumping');
               //populate form
               $this->viewParams['form']->populate($data['search_params']);
           	}
        }

        if(isset($paginator))
       	{                
            $this->viewParams['paginator'] = $paginator;
    		$this->viewParams['commentList'] = $paginator->getCurrentItems();//list of found comments
    		$this->viewParams['formComment'] = new comment_Form_Comment();
//        	$this->viewParams['formComment']->cluBuild(array('action' => 'new','control' => 'ajax'));
        	
        	$actionUrl = "/" . $sess->viewLang . "/comment/ajax/new-comment";
        	$this->viewParams['formComment']->cluBuild(array('action' => 'new' , 'action_url' => $actionUrl, 
        												  'control' => 'ajax',
                                                           ));
            //$this->renderToHolder('index/list.phtml',false, null, 4,2); 
       	}
        	//$this->renderToHolder('index/search.phtml',false, null, 4,1); //search box 
    }
	public function successAction()
    {
         $this->view->commentId =  $this->_request->getParam("lastInsertId");
         $this->renderToHolder( 'common/success.phtml');  
    }
    
}
?>
